package com.hc.dfs.tool;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.Html;
import android.text.Spanned;

public class StringHandle {

	public static boolean isNotEmptyString(String str){
		if(str == null){
			return false;
		}
		if(str.trim().equals("")){
			return false;
		}
		return true;
	}
	
	public static String EnsureMaximumLength(String str,int maxLength){
		if(str == null) return "";
		if(str.length() >maxLength){
			return str.substring(maxLength);
		}
		return str;
	}
	
	public static boolean isEnsureLength(String str ,int minLength,int maxLength){
		if(str != null){
			int len = str.trim().length();
			if(len >=minLength && len<=maxLength){
				return true;
			}
		}
		return false;
	}
	
	public static boolean isValidEmail(String str){
		Pattern p = Pattern.compile("^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$",Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(str);
		return m.matches();
	}
	
	public static boolean checkUserName(String str){
		Pattern p = Pattern.compile("^[_a-zA-Z0-9\\u4e00-\\u9fa5]+$",Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(str);
		return m.matches() && !isNumber(str);
	}
	
	public static boolean checkPhone(String str){
		Pattern p = Pattern.compile("0?(13|15|18)[0-9]{9}$",Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(str);
		return m.matches();
	}
	
	public static boolean isEmptyString(String str){
		return !isNotEmptyString(str);
	}
	
	public static boolean isNumber(String str){
		if(isNotEmptyString(str)){
			try {
				Integer.parseInt(str);
				return true;
			} catch (NumberFormatException e) {
			}
		}
		return false;
	}
	public static Spanned getBlackFont(String str){
		return  Html.fromHtml(String.format("<font color='#000'>%s</font>", str));
	}
}
